package com.rinlink.jbcodec.transform;

import com.rinlink.jbcodec.exception.TransformException;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public abstract class BytesToList<T> extends BytesToObject<List<T>> {

    private int listSize;

    public BytesToList(ByteBuffer byteBuffer,int listSize) {
        super(byteBuffer);
        this.listSize = listSize;
    }

    @Override
    public List<T> transform() throws TransformException {
        List<T> list = new ArrayList<>(listSize);

        ByteBuffer buf = getByteBuffer();
        for (int i = 0; i < listSize; i ++){
            T t  = transform(buf);
            list.add(t);
        }

        return list;
    }

    protected abstract T transform(ByteBuffer buffer);

}
